import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_widgets/resources/images.dart';
import 'package:kq_flutter_core_widget/utils/native/file_picker_native_util.dart';
import 'package:kq_flutter_core_widget/utils/platform.dart';
import 'package:kq_flutter_widgets/utils/permission_util.dart';
import 'package:kq_flutter_widgets/widgets/bottomSheet/kq_botton_sheet_manger.dart';
import 'package:kq_flutter_widgets/widgets/bottomSheet/sheets/kq_action_list_bottom_sheet.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../config/kq_global.dart';
import '../../resources/l10n/kq_intl.dart';

/// 文件选择器
class KqFilePicker {
  int maxCount;
  int? compress;

  /// 构建文件选择器
  /// [compress]指定图片压缩后的大小，[compress]为null或[compress]<=0时不压缩，图片本身大小 <= [compress]时也不压缩，
  /// 如果选择的文件非图片也不压缩，单位KB
  KqFilePicker({this.maxCount = 1, this.compress = 2048});

  /// 选择图片
  void choosePhoto(
      BuildContext context, Function(List<KqFile> files)? callBack) {
    KqBottomSheetManager.showActionList(
      [
        KqActionListData(
            item: KqIntl.currentResource.takePhoto,
            icon: Images.commonIcCamera16Black,
            iconPackage: KqGlobal.packageName),
        KqActionListData(
            item: KqIntl.currentResource.choosePhoto,
            icon: Images.commonIcPhoto16Black,
            iconPackage: KqGlobal.packageName)
      ],
      alignment: Alignment.centerLeft,
      callback: ((index, item) async {
        if (index == 1) {
          // 相册
          _chooseFile(true, callBack: callBack);
        } else {
          // 拍照
          _takePhoto(callBack);
        }
      }),
    );
  }

  _chooseFile(isImage,
      {List<String>? extensions, Function(List<KqFile> file)? callBack}) async {
    Permission? permission;
    if (PlatformEx.isAndroid) {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      if (androidInfo.version.sdkInt > 29) {
        permission = Permission.manageExternalStorage;
      } else {
        permission = Permission.storage;
      }
    } else if (PlatformEx.isIOS) {
      permission = Permission.photos;
    }

    if (permission == null) {
      _chooseFile2(isImage, extensions: extensions, callBack: callBack);
      return;
    }

    var status = await PermissionUtils.request(
      permission,
      applyTitle: KqIntl.currentResource.applyPermissionTitle,
      applyDesc: isImage
          ? KqIntl.currentResource.applyAlbumPermissionHint
          : KqIntl.currentResource.applyStoragePermissionHint,
      permanentlyDeniedDesc: isImage
          ? KqIntl.currentResource.permanentlyDeniedAlbumPermissionHint
          : KqIntl.currentResource.permanentlyDeniedStoragePermissionHint,
    );
    if (status != PermissionStatus.granted) {
      return;
    }

    _chooseFile2(isImage, extensions: extensions, callBack: callBack);
  }

  _chooseFile2(isImage,
      {List<String>? extensions, Function(List<KqFile> file)? callBack}) async {
    if (isImage) {
      var files = await FilePickerNativeUtil.pickImages(
          maxCount: maxCount, compress: compress);
      if (callBack != null) {
        callBack(files);
      }
    } else {
      var files = await FilePickerNativeUtil.pickFiles(
          maxCount: maxCount, extensions: extensions, compress: compress);
      if (callBack != null) {
        callBack(files);
      }
    }
  }

  _takePhoto([Function(List<KqFile> file)? callBack]) async {
    var status = await PermissionUtils.request(
      Permission.camera,
      applyTitle: KqIntl.currentResource.applyPermissionTitle,
      applyDesc: KqIntl.currentResource.applyCameraPermissionHint,
      permanentlyDeniedDesc:
          KqIntl.currentResource.permanentlyCameraPermissionHint,
    );

    if (status != PermissionStatus.granted) {
      return;
    }
    _takePhoto2(callBack);
  }

  _takePhoto2([Function(List<KqFile> file)? callBack]) async {
    var file = await FilePickerNativeUtil.takePhoto(compress: compress);
    if (callBack != null) {
      callBack(file != null ? [file] : []);
    }
  }

  /// 选择文件
  ///
  /// [extensions]为文件后缀，指定哪些类型文件可选，不传则表示所有文件可选。空列表则不能选择任何文件
  void chooseFile(BuildContext context,
      {List<String>? extensions, Function(List<KqFile> file)? callBack}) {
    KqBottomSheetManager.showActionList(
      [
        KqActionListData(
            item: KqIntl.currentResource.takePhoto,
            icon: Images.commonIcCamera16Black,
            iconPackage: KqGlobal.packageName),
        KqActionListData(
            item: KqIntl.currentResource.chooseFile,
            icon: Images.commonIcFile16Black,
            iconPackage: KqGlobal.packageName)
      ],
      alignment: Alignment.centerLeft,
      callback: ((index, item) async {
        if (index == 1) {
          _chooseFile(false, extensions: extensions, callBack: callBack);
        } else {
          _takePhoto(callBack);
        }
      }),
    );
  }
}
